package cn.iocoder.yudao.module.wms.service.customer;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.customer.vo.CustomerPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.customer.vo.CustomerSaveReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.inventory.vo.InventorySaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.customer.CustomerDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.customerclassify.CustomerClassifyDO;
import cn.iocoder.yudao.module.wms.dal.dataobject.inventory.InventoryDO;
import cn.iocoder.yudao.module.wms.dal.mysql.customer.CustomerMapper;
import cn.iocoder.yudao.module.wms.service.customerclassify.CustomerClassifyService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.*;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.INVENTORY_CODE_EXISTS;

/**
 * 客户档案 Service 实现类
 *
 * @author 刘锦
 */
@Service
@Validated
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private CustomerClassifyService customerClassifyService;

    @Override
    public Long createCustomer(CustomerSaveReqVO createReqVO) {
        validateCodeOrNameExists(createReqVO);
        // 插入
        CustomerDO customer = BeanUtils.toBean(createReqVO, CustomerDO.class);
        customerMapper.insert(customer);
        // 返回
        return customer.getId();
    }

    @Override
    public void updateCustomer(CustomerSaveReqVO updateReqVO) {
        // 校验存在
        validateCustomerExists(updateReqVO.getId());
        validateCodeOrNameExists(updateReqVO);
        // 更新
        CustomerDO updateObj = BeanUtils.toBean(updateReqVO, CustomerDO.class);
        customerMapper.updateById(updateObj);
    }

    @Override
    public void deleteCustomer(Long id) {
        // 校验存在
        validateCustomerExists(id);
        // 删除
        customerMapper.deleteById(id);
    }

    private void validateCustomerExists(Long id) {
        if (customerMapper.selectById(id) == null) {
            throw exception(CUSTOMER_NOT_EXISTS);
        }
    }

    /**
     * 校验编码或名称是否存在
     * @param reqVO 客户Request VO
     */
    private void validateCodeOrNameExists(CustomerSaveReqVO reqVO) {
        if (customerMapper.selectCount(CustomerDO::getCode, reqVO.getCode()) > 0) {
            throw exception(CUSTOMER_CODE_EXISTS);
        }
        if (customerMapper.selectCount(CustomerDO::getName, reqVO.getName()) > 0) {
            throw exception(CUSTOMER_NAME_EXISTS);
        }
    }

    @Override
    public CustomerDO getCustomer(Long id) {
        return customerMapper.selectById(id);
    }

    @Override
    public PageResult<CustomerDO> getCustomerPage(CustomerPageReqVO pageReqVO) {
        return customerMapper.selectPage(pageReqVO, getClassifyCondition(pageReqVO.getClassifyId()));
    }

    @Override
    public List<CustomerDO> getCustomerSimpleList(String query) {
        return customerMapper.selectList(query);
    }

    /**
     * 获得存货分类条件：查询指定存货分类的子分类编号们，包括自身
     *
     * @param classifyId 存货分类编号
     * @return 存货分类编号集合
     */
    private Set<Long> getClassifyCondition(Long classifyId) {
        if (classifyId == null) {
            return Collections.emptySet();
        }
        Set<Long> classifyIds = convertSet(customerClassifyService.getChildCustomerClassifyList(classifyId), CustomerClassifyDO::getId);
        classifyIds.add(classifyId); // 包括自身
        return classifyIds;
    }

}